# usbrebind script to remap the AG5 for easier gaming
#
# The only sensible directional pad on the AG5 is the CYKL cluster.
# Unfortunately, this cluster and the AG5's normal mouse buttons are
# mutually exclusive.  Most any game typically played using the traditional
# WASD mapping on a QWERTY keyboard will require (or at least strongly encourage)
# the mouse buttons to be pressed while using the directional pad.  In other
# words, you want to be able to shoot while moving.  World of Warcraft is even
# worse, in that it requires holding down mouse buttons in order to change the
# camera view.  Not being able to do this while moving makes playing the game
# nearly impossible.
#
# This script remaps the T and N keys to serve as left and right mouse clicks.
# It also remaps PageUp and Home (which are generated using RedShift + Enter or Space)
# to be wheel up and down, since the AG5 doesn't really provide a good wheel, and
# many games rely on the wheel for various functions.
#
# The PrintScreen key is used to toggle the remaps on and off, so that the original
# keys can still be generated when normal typing is necessary.

# Whether or not to execute remaps
$remaps_on = true

# Handle events
def UHK_USB_keyev( code, evtype )
  
  # Decide what to do based on key code
  case code
    
    # Toggle remaps on PrintScreen
    when USB_PrintScreen
      if evtype == 0
        $remaps_on = !( $remaps_on )
      end
    
    # For everything else
    else
      
      # If remaps are active
      if $remaps_on
        case code
          
          # Turn T and N into left and right click
          when USB_t then ( UHK_btnev( 1, evtype ); return )
          when USB_n then ( UHK_btnev( 3, evtype ); return )
          
          # Turn R and H into L and R control
          when USB_r then ( UHK_keyev( WIN_Control_L, evtype ); return )
          when USB_h then ( UHK_keyev( WIN_Control_R, evtype ); return )
          
          # Turn PageUp/Home (RShift + Enter/Space on AG5) into MWheel Up/Down
          # Don't do anything here - mark function has already taken care of it
          #when USB_PageUp then ( UHK_btnev( 4, evtype ); return )
          #when USB_Home   then ( UHK_btnev( 5, evtype ); return )
          when USB_PageUp then return
          when USB_Home   then return
        end
      end
        
      # Pass everything else through
      UHK_keyev( UHK_get_keysym( code ), evtype )
  end
end

# Handle mousewheel
def UHK_USB_mark( event, elapsed )
  
  # If remaps are active
  if $remaps_on
  
    # If this is an idle event or a start release event
    if ( event == MARK_START_RELEASE || event == MARK_IDLE )
    
      # If PageUp is held down, generate MWheelUp
      if UHK_is_key_pressed( USB_PageUp ) > 0
        UHK_btndn( 4 )
        UHK_btnup( 4 )
      
      # If Home is held down, generate MWheelDown
      elsif UHK_is_key_pressed( USB_Home ) > 0
        UHK_btndn( 5 )
        UHK_btnup( 5 )
      end
    end
  end
end

# Connect to the alphagrip if present.
UHK_connect( 0x510, 0x100b, 0x81 )
